package com.itic.web.util;

import com.itic.timer.demo.GlobalToken;
import com.itic.web.qo.TokenData;

public class TokenUtil {
	
	
	//返回用户当天的token
	public static String getUserToken(String account){
		String globalToken = GlobalToken.getAppToken();
    	//加密
    	String hex = Toolkit.bytes2Hex(MD5Util.string2bytes(MD5Util.convertMD5(account + ";" + globalToken),"UTF-8"));
    	System.out.println("加密后转16进制的：" + hex);
    	//加密后把第6位进1
/*    	hex = HexUtil.updateHex(hex);
    	System.out.println("加密后16进制再加盐后的：" + hex);*/
    	return hex;
	}
	public static String getPassWord(String account,String VerificationCode,String expireTime){
		//加密
		String passwordhex = Toolkit.bytes2Hex(MD5Util.string2bytes(MD5Util.convertMD5(account + ";" + VerificationCode + ";" + expireTime),"UTF-8"));
		System.out.println("加密后转16进制的：passwordhex = " + passwordhex);
		//加密后把第6位进1
/*		passwordhex = HexUtil.updateHex(passwordhex);
		System.out.println("加密后16进制再加盐后的：passwordhex" + passwordhex);*/
		return passwordhex;
	}
	
	//返回用户名和口令
	public static TokenData getUserList(String userToken){
		
		String[] strs = null;
		TokenData data = new TokenData();
		try {
			
			//解密
/*			String backhex = HexUtil.backHex(userToken);
			System.out.println("解密去盐后的：" + backhex);*/
			String back = MD5Util.convertMD5(MD5Util.bytes2string(Toolkit.hex2Bytes(userToken),"UTF-8"));
			System.out.println("解密后十进制的：" + back);
			strs = back.split(";");
		} catch (Exception e) {
           e.printStackTrace();
		}
    	data.setAccount(strs[0].toString());
    	System.out.println("解析出来的帐号信息是"+strs[0].toString());
    	data.setToken(strs[1].toString());
    	System.out.println("解析出来的口令信息是"+strs[1].toString());
    	return data;
	}
	//返回用户名和口令
	public static boolean assertUserToken(TokenData data){
		boolean flag = false;
		//判断每日口令是不是正确
		
		String globalToken = GlobalToken.getAppToken();
		try {
			TokenData userList = getUserList(data.getToken());
			String coreToken = userList.getToken();
			if(globalToken.equals(coreToken)){
				flag = true;
			}
		} catch (Exception e) {
			flag = false;
		}
		
		return flag;
	}

}
